#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mongodriver
import json
import sys

class AnagraphData:
    def __init__(self):
        self.mongo_driver = mongodriver.MongoDriver()

    def getAnagraphs(self, ana_name, fields = []):
        reversed_names = dict((value,key) for key,value in self.mongo_driver.collect_names.iteritems())

        if not reversed_names.has_key(unicode(ana_name)):
            return []
        else:
            col_name    = reversed_names[unicode(ana_name)]
            col         = self.mongo_driver.db[col_name]

            if len(fields) != 0:
                fields = dict((key, 1) for key in fields)
            else:
                fields = {}

            query_datas = col.find({}, fields)
            ana_datas   = []

            for query_item in query_datas:
                ana_item = {}

                if not query_item.has_key('name'):
                    print query_item
                    sys.exit()

                ana_item['name']        = ana_name + ":" + query_item['name']
                ana_item['medicines']   = query_item['medicines']

                ana_datas.append(ana_item)

            return ana_datas
